package kaoshi.servlet_2;

import kaoshi.entity.person;
import kaoshi.service.impl.personServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/search.do")
public class SelectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //设置响应的格式
        response.setContentType("text/html;charset=utf-8");
        //设置响应的编码格式
        response.setCharacterEncoding("utf-8");
        //调用后台的查询所有的方法
        PrintWriter out=response.getWriter();
        personServiceImpl impl=new personServiceImpl();
        List<person> list=impl.selAll();
        //把list集合中的数据 组装成json格式
        StringBuffer sb=new StringBuffer();
        sb.append("[");
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).toString());
            if (i!=list.size()-1){
                sb.append(",");
            }
        }
        sb.append("]");
        out.println(sb);
        out.flush();
        out.close();
        //跳转到页面
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
